<?php

namespace Home\Model;

use Think\Model;

class GoodsModel extends Model {

    function get_info($id) {
        $goods = $this->field(C('DB_PREFIX') . 'store.state,' . C('DB_PREFIX') . 'goods.*')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'store  ON ' . C('DB_PREFIX') . 'goods.store_id = ' . C('DB_PREFIX') . 'store.store_id')
                ->where('goods_id=' . $id)
                ->find();
        if ($goods) {
            /* 商品规格 */
            $Spec_mod = M('goods_spec');
            $specs = $Spec_mod->where("goods_id = '$id'")->select();
            $other_spec = array();
            foreach ($specs as $spec) {
                if ($spec['spec_id'] != $goods['default_spec']) {
                    if ($goods['market_price'] > 0) {
                        $spec['market_price'] = $goods['market_price'] * $spec['price'];
                    }
                    $other_spec[] = $spec;
                } else {
                    $default_spec = $spec;
                }
            }

            if ($goods['market_price'] > 0) {
                $default_spec['market_price'] = $goods['market_price'] * $default_spec['price'];
            }
            $goods['_specs'] = array_merge(array($default_spec), $other_spec);

            /* 商品图片 */
            $Image_mod = M('goods_image');
            $_images = $Image_mod->where("goods_id = '$id'")->order('sort_order desc')->select();
            $goods['_images'] = array_values($_images);
            
            /* 统计情况 */
            $stat_info = M('goods_statistics')->where('goods_id='.$id)->find();
            $goods = array_merge($goods, $stat_info);
        }
        return $goods;
    }

    function get_infos($id){
        $goods = $this->field(C('DB_PREFIX') . 'store.state,' . C('DB_PREFIX') . 'goods.*,'. 'goods_spec.*')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'store  ON ' . C('DB_PREFIX') . 'goods.store_id = ' . C('DB_PREFIX') . 'store.store_id')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'spec  ON ' . C('DB_PREFIX') . 'goods.goods_id = ' . C('DB_PREFIX') . 'spec.goods_id')
            ->where('spec_id=' . $id)
            ->find();

        return $goods;
    }

}

?>
